<?php

class Admin_Model_Media_Mapper extends Admin_Model_MapperAbstract {
	public function __construct($db) {
		parent::__construct($db);
		
		$this->setDbTable(new Admin_Model_Media_DbTable(array(Admin_Model_Media_DbTable::ADAPTER => $db)));
	}
	/**
	 * 
	 * @param Admin_Model_User $model
	 * @return type
	 */
	public function save(Admin_Model_Media_Media $model) {
		$Data = array(
			'ID' => $model->getId(),
			'FileName' => $model->getFileName(),
			'FileType' => $model->getFileType(),
			'Created' => $model->getCreated(),
			'Meta' => $model->getMeta()
		);

		return parent::save($Data);
	}

	/**
	 * 
	 * @param type $id
	 * @return \Admin_Model_Post_Post
	 */
	public function find($id) {
		$Result = parent::find($id);
		if (null === $Result) {
			return;
		}

		$Post = new Admin_Model_Media_Media();
		$Post->setId($Result->ID)
				->setFileName($Result->Title)
				->setFileType($Result->Content)
				->setCreated($Result->Created)
				->setMeta($Result->Meta);

		return $Post;
	}
	
	/**
	 * 
	 * @return array
	 */
	public function fetchAll($where = null, $order = null, $count = null, $offset = null) {
		//$Rows = parent::fetchAll($where, $order, $count, $offset);
		$Rows = $this->getDbTable()
						->select()
						->from($this->getDbTable()->info(Zend_Db_Table_Abstract::NAME))
						->join('w_user', 'w_user.ID = ' . $this->getDbTable()->info(Zend_Db_Table_Abstract::NAME) . '.ID', 'UserName')
						->where($where)
						->order($order);
		$Posts = array();
		foreach ($Rows as $Row) {
			$Post = new Admin_Model_Post_Post();
			$Post->setId($Row->ID)
				->setTitle($Row->Title)
				->setContent($Row->Content)
				->setStatus($Row->Status)
				->setCreated($Row->Created)
				->setUserId($Row->UserID)
				->setModified($Row->Modified)
				->setMeta($Row->Meta)
				->setPublished($Row->Published);
		}
		
		return $Posts;
	}
	
	protected function parseSelect($condition = array(), $order = null) {
		$Select = $this->getDbTable()->select()->from($this->getDbTable()->info(Zend_Db_Table_Abstract::NAME));//->where($condition['where'], $condition['value'], $condition['type'])->order($order);
		//$Select->join('w_user', 'w_post.ID = w_user.ID');
		if (is_array($condition)) {
			foreach ($condition as $v) {
				if (is_array($v)) {
					$Select->where($v['where'], $v['value'], $v['type']);
				}
			}
		}
		
		$Select->order($order);
		
		return $Select;
	}


	/*public function fetchPagination($where = null, $order = null) {
		$Paginator = parent::fetchPagination($where, $order);
		
		return $Paginator;
	}*/

}
